//
//  LZCheckoutView.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/12/23.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZCheckoutView.h"
#import "LZMyCouponsTableViewCell.h"
#import "LZMyCouponsModel.h"
#import "LZUserModel.h"
#import "AESCrypt.h"
#import "Masonry.h"

@interface LZCheckoutView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView * mainBackgroundView;
@property (nonatomic, strong) UIButton *confirmButton;

// ********************* 列表里无数据的view *********************
@property (nonatomic, strong) UIView *noDataView;

// ********************* 优惠券列表UI里的view *********************
@property (nonatomic, strong) UILabel *labelTitle;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UITableView *table;

// ********************* 二维码UI里的view *********************
@property (nonatomic, strong) UIImageView *imgQRCode;
@property (nonatomic, strong) UILabel *labelQRRemind;

// ********************* 扫描成功UI里的view *********************
@property (nonatomic, strong) UIView *successView;
@property (nonatomic, strong) UIButton *laterButton;

@property (nonatomic, strong) NSArray *arrData;

@property (nonatomic, copy) NSIndexPath *selectedIndexPath;

@property (nonatomic, assign) PayCouponViewState state;   // Default == 1, 记录当前走到了第几步，默认是第一步

@property (nonatomic, copy) NSString *promotionId;

@property (nonatomic, copy) NSString *sellerId;

@property (nonatomic, copy) NSString *noRemarkId;

@end

@implementation LZCheckoutView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.backgroundColor = UIColorFromRGB(0x000000, 0.7);
//        [self configEffectView];
        [self configBackgroundView];
        [self configCouponListView];
        [self configQRCodeView];
        [self hiddenQRCodeViews:YES];
        
        self.hidden = YES;
        self.alpha = 0;
    }
    return self;
}


#pragma
#pragma mark - private method

- (void)configEffectView {
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
    [self addSubview:effectView];
    
    [effectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}

- (void)configBackgroundView {
    
    // ********************** mainBackgroundView **********************
    self.mainBackgroundView = [[UIView alloc] init];
    self.mainBackgroundView.backgroundColor = [UIColor whiteColor];
    self.mainBackgroundView.layer.cornerRadius = 4;
    self.mainBackgroundView.layer.masksToBounds = YES;
    [self addSubview:self.mainBackgroundView];
    
    [self.mainBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self).offset(25);
        make.trailing.equalTo(self).offset(-25);
        make.top.equalTo(self).offset(70);
        make.bottom.equalTo(self).offset(-62);
    }];
    
    
    
    // ********************** 右上角的X按钮 **********************
    UIButton *btnCancel = [[UIButton alloc] init];
    [btnCancel setImage:[UIImage imageNamed:@"preferential_close"] forState:UIControlStateNormal];
    [btnCancel addTarget:self action:@selector(clickCancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btnCancel];
    
    [btnCancel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.and.height.equalTo(50);
        make.centerX.equalTo(self.mainBackgroundView.mas_trailing).offset(-3);
        make.centerY.equalTo(self.mainBackgroundView.mas_top).offset(3);
    }];
    
    // ********************** 下边的Confirm按钮 **********************
    self.confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.confirmButton.backgroundColor = UIColorFromRGB(0xcccccc, 1);
    [self.confirmButton setTitle:LZLocalizedString(@"confirm_capital", nil) forState:UIControlStateNormal];
    self.confirmButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.confirmButton addTarget:self action:@selector(clickConfirmButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.confirmButton.userInteractionEnabled = NO;
    [self.mainBackgroundView addSubview:self.confirmButton];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.and.trailing.and.bottom.equalTo(self.mainBackgroundView);
        make.height.equalTo(51);
    }];
}

#pragma mark 初始化没有数据的view
- (void)configNoDataView {
    
    GET_SCREEN_SCALE(scale)

    self.noDataView = [[UIView alloc] init];
    [self.mainBackgroundView addSubview:self.noDataView];
    
    [self.noDataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.and.trailing.equalTo(0);
        make.top.equalTo(self.line.mas_bottom);
        make.bottom.equalTo(0);
    }];
    
    UIView *bgView = [[UIView alloc] init];
    [self.noDataView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.and.trailing.equalTo(0);
        make.centerY.equalTo(self.noDataView).offset(-25*scale);
    }];
    
    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.image = [UIImage imageNamed:@"restaurant_no_coupon"];
    [bgView addSubview:imgView];
    
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(0);
        make.centerX.equalTo(0);
    }];


    UILabel *lab = [[UILabel alloc] init];
    lab.textAlignment = NSTextAlignmentCenter;
    lab.text = LZLocalizedString(@"restaurant_no_coupon_remind", nil);
    lab.font = [UIFont systemFontOfSize:13*scale];
    lab.textColor = UIColorFromRGB(0x999999, 1);
    lab.numberOfLines = 0;
    [bgView addSubview:lab];
    
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(50);
        make.trailing.equalTo(-50);
        make.centerX.equalTo(0);
        make.top.equalTo(imgView.mas_bottom).offset(20*scale);
    }];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(lab.mas_bottom);
    }];
}

#pragma mark 初始化优惠券列表（第一步的view）
- (void)configCouponListView {
    // ********************** 上边的标题 **********************
    self.labelTitle = [[UILabel alloc] init];
    // 选择优惠(标题)
    self.labelTitle.text = LZLocalizedString(@"restaurant_select_coupon", nil);
    self.labelTitle.numberOfLines = 0;
    self.labelTitle.font = [UIFont systemFontOfSize:15];
    self.labelTitle.textColor = UIColorFromRGB(0x333333, 1);
    self.labelTitle.textAlignment = NSTextAlignmentCenter;
    [self.mainBackgroundView addSubview:self.labelTitle];
    [self.labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainBackgroundView).offset(20);
        make.centerX.equalTo(self.mainBackgroundView);
        make.leading.equalTo(self.mainBackgroundView).offset(64*SCREEN_WIDTH/375.0);
        make.trailing.equalTo(self.mainBackgroundView).offset(-64*SCREEN_WIDTH/375.0);
    }];
    
    // ********************** 标题下边的线 **********************
    self.line = [[UIView alloc] init];
    self.line.backgroundColor = UIColorFromRGB(0xdddddd, 1);
    [self.mainBackgroundView addSubview:self.line];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.mainBackgroundView).offset(10);
        make.trailing.equalTo(self.mainBackgroundView).offset(-10);
        make.top.equalTo(self.labelTitle.mas_bottom).offset(20);
        make.height.equalTo(0.5);
    }];
    
    
    
    // ********************** TableView **********************
    [self.mainBackgroundView addSubview:self.table];
    
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.and.trailing.equalTo(self.mainBackgroundView);
        make.top.equalTo(self.line.mas_bottom);
        make.bottom.equalTo(self.confirmButton.mas_top);
    }];
    
    // ********************** TableView上边的遮罩 **********************
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"selectCoupon_缓冲蒙版"];
    [self.mainBackgroundView addSubview:imageView];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line.mas_bottom);
        make.leading.and.trailing.equalTo(self.mainBackgroundView);
        make.height.equalTo(20);
    }];
}

#pragma mark 初始化二维码的view（第二步）
- (void)configQRCodeView {
    // ********************** 二维码图片 **********************
    self.imgQRCode = [[UIImageView alloc] init];
    self.imgQRCode.hidden = YES;
    [self.mainBackgroundView addSubview:self.imgQRCode];
    
    [self.imgQRCode mas_makeConstraints:^(MASConstraintMaker *make) {
        if (IS_IPHONE_4) {
            make.top.equalTo(self.mainBackgroundView).offset(30);
        }
        else {
            make.top.equalTo(self.mainBackgroundView).offset(111*SCREEN_WIDTH/375.0);
        }
        make.leading.equalTo(self.mainBackgroundView).offset(51*SCREEN_WIDTH/375.0);
        make.trailing.equalTo(self.mainBackgroundView).offset(-51*SCREEN_WIDTH/375.0);
        make.height.equalTo(self.imgQRCode.width).multipliedBy(1);
    }];
    
    // ********************** 二维码图片下边的提示语 **********************
    self.labelQRRemind = [[UILabel alloc] init];
    // 请让商家扫描并确认此当前二维码
    self.labelQRRemind.text = LZLocalizedString(@"restaurant_scan_remind", nil);
    self.labelQRRemind.font = [UIFont systemFontOfSize:15];
    self.labelQRRemind.textColor = UIColorFromRGB(0xcccccc, 1);
    self.labelQRRemind.numberOfLines = 0;
    self.labelQRRemind.textAlignment = NSTextAlignmentCenter;
    [self.mainBackgroundView addSubview:self.labelQRRemind];
    
    [self.labelQRRemind mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.and.trailing.equalTo(self.imgQRCode);
        make.top.equalTo(self.imgQRCode.mas_bottom).offset(20);
    }];
}

#pragma mark  初始化扫描成功的view（第三步）
- (void)configScanSuccessView {
    
    GET_SCREEN_SCALE(scale)
    
    self.successView = [[UIView alloc] init];
    [self.mainBackgroundView addSubview:self.successView];
    [self.successView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.mainBackgroundView);
    }];
    
    // ********************** 成功的对勾图片 **********************
    UIImageView *imgSuccess = [[UIImageView alloc] init];
    imgSuccess.image = [UIImage imageNamed:@"pay_success"];
    [self.successView addSubview:imgSuccess];
    
    [imgSuccess mas_makeConstraints:^(MASConstraintMaker *make) {
        if (IS_IPHONE_4) {
            make.top.equalTo(15);
        }
        else {
            make.top.equalTo(50*scale);
        }
        make.centerX.equalTo(self.mainBackgroundView);
    }];
    
    // ********************** 成功的对勾图片下边的提示语 **********************
    UILabel *labelSuccessRemind = [[UILabel alloc] init];
    // 优惠买单成功，欢迎您再次光临！(弹出框提示语)
    labelSuccessRemind.text = LZLocalizedString(@"restaurant_pay_success_remind", nil);
    labelSuccessRemind.numberOfLines = 0;
    labelSuccessRemind.font = [UIFont systemFontOfSize:15*scale];
    labelSuccessRemind.textColor = UIColorFromRGB(0x333333, 1);
    labelSuccessRemind.textAlignment = NSTextAlignmentCenter;
    [self.successView addSubview:labelSuccessRemind];
    
    [labelSuccessRemind mas_makeConstraints:^(MASConstraintMaker *make) {
        
        if (IS_IPHONE_4) {
            make.top.equalTo(imgSuccess.mas_bottom).offset(10);
        }
        else {
            make.top.equalTo(imgSuccess.mas_bottom).offset(18*scale);
        }
        make.leading.equalTo(self.successView).offset(50);
        make.trailing.equalTo(self.successView).offset(-50);
    }];
    
    // ********************** 提示语下边的线 **********************
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = UIColorFromRGB(0xdddddd, 1);
    [self.successView addSubview:line];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        if (IS_IPHONE_4) {
            make.top.equalTo(labelSuccessRemind.mas_bottom).offset(10);
        }
        else {
            make.top.equalTo(labelSuccessRemind.mas_bottom).offset(20*scale);
        }
        make.leading.equalTo(self.successView).offset(50);
        make.trailing.equalTo(self.successView).offset(-50);
        make.height.equalTo(0.5);
    }];
    
    // ********************** Later按钮 **********************
    self.laterButton = [UIButton buttonWithType:UIButtonTypeCustom];
    // 暂不评价(按钮) 
    [self.laterButton setTitle:LZLocalizedString(@"confirm_capital", nil) forState:UIControlStateNormal];
    [self.laterButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.laterButton.backgroundColor = UIColorFromRGB(0xf0872e, 1);
    self.laterButton.titleLabel.font = self.confirmButton.titleLabel.font;
    [self.mainBackgroundView addSubview:self.laterButton];
    [self.laterButton addTarget:self action:@selector(clickLaterButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.laterButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.and.trailing.equalTo(0);
        make.bottom.and.height.equalTo(self.confirmButton);
    }];
    
    // ********************** 线下边的view **********************
    
    UIView *centerBgView = [[UIView alloc] init];
    [self.successView addSubview:centerBgView];
    
    [centerBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom);
        make.bottom.equalTo(self.laterButton.mas_top);
        make.leading.and.trailing.equalTo(0);
    }];
    
    UIView *centerView = [[UIView alloc] init];
    [centerBgView addSubview:centerView];
    
    [centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.and.trailing.equalTo(0);
        make.centerY.equalTo(centerBgView.centerY);
    }];
    
    // ********************** 线下边的图片 **********************
    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.image = [UIImage imageNamed:@"pay_success_center_image"];
    [centerView addSubview:imgView];
    
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(0);
        make.centerX.equalTo(centerView.mas_centerX);
        make.size.equalTo(CGSizeMake(238/2.0*scale, 263./2.0*scale));
    }];
    
    // ********************** 最下边的提示语 **********************
    UILabel *labelSuccessBottomRemind = [[UILabel alloc] init];
    // 您可以对菜品和餐厅做出评价。(提示语)
    labelSuccessBottomRemind.text = LZLocalizedString(@"restaurant_share_remind", nil);
    labelSuccessBottomRemind.numberOfLines = 0;
    labelSuccessBottomRemind.font = [UIFont systemFontOfSize:12*scale];
    labelSuccessBottomRemind.textColor = UIColorFromRGB(0x666666, 1);
    labelSuccessBottomRemind.textAlignment = NSTextAlignmentCenter;
    [centerView addSubview:labelSuccessBottomRemind];
    
    [labelSuccessBottomRemind mas_makeConstraints:^(MASConstraintMaker *make) {
        if (IS_IPHONE_4) {
            make.top.equalTo(imgView.mas_bottom).offset(10);
        }
        else {
            make.top.equalTo(imgView.mas_bottom).offset(20*scale);
        }
        make.leading.equalTo(centerView).offset(77);
        make.trailing.equalTo(centerView).offset(-77);
    }];
    
    UIButton * btnReview = [[UIButton alloc] init];
    btnReview.layer.cornerRadius = 15.5;
    btnReview.layer.masksToBounds = YES;
    btnReview.layer.borderWidth = 0.5;
    btnReview.layer.borderColor = UIColorFromRGB(0xff7a21, 1).CGColor;
    [btnReview setTitle:LZLocalizedString(@"restaurant_review", nil) forState:UIControlStateNormal];
    [btnReview setTitleColor:UIColorFromRGB(0xff7a21, 1) forState:UIControlStateNormal];
    btnReview.titleLabel.font = [UIFont systemFontOfSize:15*scale];
    [btnReview addTarget:self action:@selector(clickReviewButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [centerView addSubview:btnReview];
    
    [btnReview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(centerView.centerX);
        make.width.equalTo(75);
        make.height.equalTo(31);
        
        if (IS_IPHONE_4) {
            make.top.equalTo(labelSuccessBottomRemind.mas_bottom).offset(10);
        }
        else {
            make.top.equalTo(labelSuccessBottomRemind.mas_bottom).offset(17*scale);
        }
    }];
    
    [centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(btnReview.mas_bottom);
    }];
}

- (UITableView*)table {
    if (_table) {
        return _table;
    }
    
    _table = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _table.delegate = self;
    _table.dataSource = self;
    _table.backgroundColor = UIColorFromRGB(0xffffff, 1);
    _table.separatorColor = [UIColor clearColor];
    
    return _table;
}

#pragma mark 按钮点击事件

// 关闭按钮点击事件
- (void)clickCancelButtonAction:(UIButton*)sender {
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        self.state = PayCouponViewState_Remove;
        [self removeFromSuperview];
    }];
}

// 确认按钮点击事件
- (void)clickConfirmButtonAction:(UIButton*)sender {
    if (self.state == PayCouponViewState_SelectCoupon) {
        // 生成二维码
        self.state = PayCouponViewState_ShowQRCode;
    }
    else if (self.state == PayCouponViewState_ShowQRCode) {
        
        self.state = PayCouponViewState_SelectCoupon;
    }
}

// Later按钮点击事件
- (void)clickLaterButtonAction:(UIButton*)sender {
    [self clickCancelButtonAction:nil];
}

// Review按钮点击事件
- (void)clickReviewButtonAction:(UIButton*)sender {
    if (self.reviewBlock) {
        self.reviewBlock(self.sellerId,self.promotionId,self.noRemarkId);
    }
    [self clickCancelButtonAction:nil];
}

#pragma mark - 设置当前状态

- (void)setState:(PayCouponViewState)state {
    _state = state;
    
    if (self.stateBlock) {
        self.stateBlock(state);
    }
    
    switch (state) {
        case PayCouponViewState_BeginShow:
        {}
            break;
        case PayCouponViewState_SelectCoupon:
        {
            [self hiddenQRCodeViews:YES];
            [self hiddenCouponListViews:NO];
            [self.confirmButton setTitle:LZLocalizedString(@"confirm_capital", nil) forState:UIControlStateNormal];
        }
            break;
        case PayCouponViewState_ShowQRCode:
        {
            if (!self.isUseCouponOpen) {
                [self drawQRImage];
            }
            
            self.imgQRCode.hidden = NO;
            [self hiddenQRCodeViews:NO];
            [self hiddenCouponListViews:YES];
            // 重新选择优惠(按钮)
            [self.confirmButton setTitle:LZLocalizedString(@"restaurant_reselect_coupon", nil) forState:UIControlStateNormal];
        }
            break;
        case PayCouponViewState_PaySuccess:
        {
            if (self.successView == nil) {
                [self configScanSuccessView];
            }
            [self hiddenQRCodeViews:YES];
            [self hiddenCouponListViews:YES];
            self.confirmButton.hidden = YES;
        }
            break;
        case PayCouponViewState_Remove:
        {}
            break;
            
        default:
            break;
    }
}

- (void)hiddenCouponListViews:(BOOL)hidden {
    self.noDataView.hidden = hidden;
    self.table.hidden = hidden;
    self.line.hidden = hidden;
    self.labelTitle.hidden = hidden;
}

- (void)hiddenQRCodeViews:(BOOL)hidden {
    self.imgQRCode.hidden = hidden;
    self.labelQRRemind.hidden = hidden;
}

#pragma mark - QRCode

// 添加二维码图片
- (void)drawQRImage {
    
    NSDictionary *dictionary = @{ @"deviceId":DEVICE_ID,
                                  @"userId":[LZUserModel Instance].userId,
                                  @"promotionType":self.promotionType,
                                  @"disCountId":self.disCountNo };
    
    NSData *data=[NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];
    NSString *qrCodeInfo=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    // 对生成的json进行aes加密
    NSString *ecryptedData = [AESCrypt encrypt:qrCodeInfo password:@"xbnlazizqrcodepassword1234"];
    DLog(@"qrCodeInfo==%@",qrCodeInfo);
    
    UIImage *image = [self encodeQRImageWithContent:ecryptedData size:self.imgQRCode.frame.size];
    self.imgQRCode.image = image;
}

// 生成二维码
- (UIImage *)encodeQRImageWithContent:(NSString *)content size:(CGSize)size {
    UIImage *codeImage = nil;
    NSData *stringData = [content dataUsingEncoding: NSUTF8StringEncoding];
    
    //生成
    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [qrFilter setValue:stringData forKey:@"inputMessage"];
    [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"];
    
    UIColor *onColor = UIColorFromRGB(0x010101, 1);
    UIColor *offColor = [UIColor whiteColor];
    
    //上色
    CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor"
                                       keysAndValues:
                             @"inputImage",qrFilter.outputImage,
                             @"inputColor0",[CIColor colorWithCGColor:onColor.CGColor],
                             @"inputColor1",[CIColor colorWithCGColor:offColor.CGColor],
                             nil];
    
    CIImage *qrImage = colorFilter.outputImage;
    CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:qrImage fromRect:qrImage.extent];
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(context, kCGInterpolationNone);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
    codeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGImageRelease(cgImage);
    return codeImage;
}

#pragma mark - Request

- (void)requestPromotionListData {
    if (self.promotionPayDelegate && [self.promotionPayDelegate respondsToSelector:@selector(requestPromotionDateWithPrameter:resultSuccess:)]) {
        
        WS(weakSelf)
        [self.promotionPayDelegate requestPromotionDateWithPrameter:nil resultSuccess:^(NSArray *result) {
            if (weakSelf) {
                if (result) {
                    weakSelf.arrData = [result copy];
                    [weakSelf.table reloadData];
                    
                }
                
                if (weakSelf.arrData.count == 0) {
                    if (weakSelf.noDataView == nil) {
                        [weakSelf configNoDataView];
                    }
                    weakSelf.confirmButton.hidden = YES;
                }
                else {
                    weakSelf.noDataView.hidden = YES;
                }
            }
        }];
    }
}

#pragma
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LZMyCouponsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
    if (!cell) {
        cell = [[LZMyCouponsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CellIdentifier"];
    }
    
    if (self.selectedIndexPath && self.selectedIndexPath.section == indexPath.section && self.selectedIndexPath.row == indexPath.row) {
        cell.selectedCellMark.hidden = NO;
    }
    else {
        cell.selectedCellMark.hidden = YES;
    }
    
    LZMyCouponsModel * model = self.arrData[indexPath.row];
    if (model) {
        cell.model = model;
    }
    
    return cell;
}

#pragma
#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 96.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.selectedIndexPath.section != indexPath.section || self.selectedIndexPath.row != indexPath.row) {
        LZMyCouponsTableViewCell *oldCell = (LZMyCouponsTableViewCell*)[tableView cellForRowAtIndexPath:self.selectedIndexPath];
        oldCell.selectedCellMark.hidden = YES;
        
        LZMyCouponsTableViewCell *newCell = (LZMyCouponsTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
        newCell.selectedCellMark.hidden = NO;
        
        self.selectedIndexPath = indexPath;
    }
    else if (self.selectedIndexPath == nil) {
        LZMyCouponsTableViewCell *newCell = (LZMyCouponsTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
        newCell.selectedCellMark.hidden = NO;
        
        self.selectedIndexPath = indexPath;
    }
    LZMyCouponsModel * model = self.arrData[indexPath.row];
    self.disCountNo = model.disCountNo;
    self.promotionType = model.promotionType;
}

- (void)setSelectedIndexPath:(NSIndexPath *)selectedIndexPath {
    if (_selectedIndexPath == nil) {
        self.confirmButton.backgroundColor = UIColorFromRGB(0xf0872e, 1);
        self.confirmButton.userInteractionEnabled = YES;
    }
    
    _selectedIndexPath = [selectedIndexPath copy];
}

#pragma
#pragma mark - public method

- (void)setIsUseCouponOpen:(BOOL)isUseCouponOpen {
    _isUseCouponOpen = isUseCouponOpen;
    if (isUseCouponOpen) {
        self.confirmButton.hidden = YES;
    }
}

- (void)showViewWithState:(PayCouponViewState)state {
    self.state = PayCouponViewState_BeginShow;
    if (state == PayCouponViewState_SelectCoupon) {
        [self requestPromotionListData];
    }
    self.hidden = NO;
    self.alpha = 0;
    self.state = state;
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
    } completion:^(BOOL finished) {
        self.state = state;
        if (state == PayCouponViewState_ShowQRCode) {
            [self drawQRImage];
        }
    }];
}

- (void)scanQRCodeSuccessWithSellerId:(NSString*)sellerId andPromotionId:(NSString*)promotionId andNoRemarkId:(NSString*)noRemarkId {
    self.sellerId = [sellerId copy];
    self.promotionId = [promotionId copy];
    self.noRemarkId = [noRemarkId copy];
    
    self.state = PayCouponViewState_PaySuccess;
    
    if (self.isHidden || self.alpha == 0) {
        self.hidden = NO;
        self.alpha = 0;
        [UIView animateWithDuration:0.3 animations:^{
            self.alpha = 1;
        }];
    }
}

@end
